use std::error::Error;
use std::process;
use steamworks::Client;

fn bytes_to_hex(vec: Vec<u8>) -> String {
    vec.iter().map(|b| format!("{:02X}", b)).collect()
}
fn hex_to_bytes(hex: String) -> Result<Vec<u8>, Box<dyn Error>> {
    let bytes: Result<Vec<u8>, _> = (0..hex.len())
        .step_by(2)
        .map(|i| u8::from_str_radix(&hex[i..i + 2], 16))
        .collect();

    bytes.map_err(|e| e.into())
}
#[test]
fn test1() {
    //初始化客户端，没开启steam客户端会报错
    let (client, _) = Client::init().unwrap();

    //获取用户接口
    let user = client.user();

    //判断是否登录了steam
    let logged_on = user.logged_on();
    if !logged_on {
        println!("没有登录steam");
        process::exit(1);
    }

    //获取用户等级
    let level = user.level();
    println!("用户等级:{level}");

    //根据steam_id获取身份凭证
    let steam_id = user.steam_id();
    let (auth_ticket, vec) = user.authentication_session_ticket_with_steam_id(steam_id);

    //测试凭证转hex_string
    let hex = bytes_to_hex(vec);
    println!("获取凭证的hex_string:{hex}");

    //测试hex_string转会字节，为之后的验证做准备
    let bytes = hex_to_bytes(hex);
    println!("凭证转字节是否成功:{}", bytes.is_ok());
    let vec = bytes.unwrap();

    //验证凭证并开启会话
    let result = user.begin_authentication_session(steam_id, &vec);
    println!("验证凭证并开启会话结果:{}", result.is_ok());

    //结束会话
    user.end_authentication_session(steam_id);

    //再次开次会话
    let result = user.begin_authentication_session(steam_id, &vec);
    println!("重新验证凭证并开启会话结果:{}", result.is_ok());

    //主动将凭证作废
    user.cancel_authentication_ticket(auth_ticket);

    //再次验证凭证并开启会话
    let result = user.begin_authentication_session(steam_id, &vec);
    println!("作废后验证凭证并开启会话结果:{}", result.is_ok());
}